package com.fastadmin.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fastadmin.config.security.annotation.AnonymousAccess;
import com.fastadmin.config.security.annotation.rest.AnonymousPostMapping;
import com.fastadmin.service.QiniuService;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.IdGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class UploadController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    private final QiniuService qiniuService;

    /**
     * 上传骑牛
     *
     * @param file input 的name 必须是file
     * @author YangKun
     * 2018年1月25日 上午12:22:10
     */
    @RequestMapping(value = "upload", produces = "application/json;charset=utf-8")
    @ResponseBody
    @AnonymousAccess
    public Object upload(@RequestParam MultipartFile file) {
        String id = IdUtil.fastUUID();
        String myFileName = file.getOriginalFilename();
        String extName = FileUtil.extName(myFileName);
        Map<String, Object> map = qiniuService.uploadQiniu(1, file, id, extName);
        map.put("url", map.get("qiniu_path"));
        logger.debug("upload image result:{}", JSON.toJSON(map));
        return buildJson(1, "1", map);
    }

}
